//
//  InvoiceClaimListVC.m
//  NtoccShipper
//
//  Created by mac on 2020/1/6.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "InvoiceClaimListVC.h"
#import "InvoiceClaimListModel.h"
#import "InvoiceClaimListCell.h"
#import "ApplyOpenInvoicListVC.h"

@interface InvoiceClaimListVC ()<UITableViewDelegate,UITableViewDataSource>{
    FlexFrameView *header;
}

@property (nonatomic,strong) InvoiceClaimListCell *invoiceClaimListCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
//@property (copy, nonatomic) NSString *strStatus; // 货源状态
@property (nonatomic,strong) CJLabel *labSumMoney;
@property (nonatomic,strong) CJLabel *labInvioceMoney;
@property (nonatomic,copy) NSString *strCodes;
@property (nonatomic, assign)float allMoney;
@end

@implementation InvoiceClaimListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cjBackgroundColor];
    DLog(@"self.indexPage======%@",self.indexPage);

    self.invoiceClaimListCell = [[InvoiceClaimListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self createTopView];
    [self.view addSubview:self.tableView];
    
    CJButton *btnSave = CJButton.btnInit
    .btnFrame(CGRectMake(0,  self.tableView.bottom,  [[UIScreen mainScreen] bounds].size.width, 44))
    .btnBgColor([UIColor cjMainColor])
    .btnText(@"确认")
    .btnFontSize(17)
    .btnTargetAction(self, @selector(btnSaveInfoAction))
    ;
    [self.view addSubview:btnSave];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

/// 创建tableview 等
- (void)createTopView{

    CGRect rcFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"InvoiceClaimListTopView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    [header layoutIfNeeded];
//    self.tableView.tableHeaderView = header;
//    [self.tableView reloadData];
    self.labSumMoney.text = @"运费金额:0.00元";
    self.labInvioceMoney.text = @"发票金额:0.00元";
    [self.view addSubview:header];
}

- (void)btnSaveInfoAction{
//    [CJAlert showStringDismissWithDelay:CJStringWithFormat(@"self.indexPage======%@",self.indexPage)];
    NSMutableArray *arrCodes = [[NSMutableArray alloc]initWithCapacity:1];
    __block  NSString *strCode = @"";
//    __block NSString *strOrderCode = @"";
    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        InvoiceClaimListModel *model = (InvoiceClaimListModel *)obj;
        if (model.selected) {
            [arrCodes addObject:model.code];
        }
    }];
    
    if (arrCodes.count > 0) {
        
        [arrCodes enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (idx == 0) {
                strCode = obj;
            }else{
                strCode = CJStringWithFormat(@"%@,%@",strCode,obj);
            }
        }];
    }
    
    if ([strCode isEqualToString:@""]) {
        [CJAlert showStringDismissWithDelay:@"请选择需要索取的发票"];
    }else{
        
        WS(ws);
        ApplyOpenInvoicListVC *vc = [ApplyOpenInvoicListVC new];
        vc.strCode = strCode;
        vc.strOpenType = @"";
        vc.blockSucess = ^(id anyObject) {
            [ws.tableView.mj_header beginRefreshing];
        };
        [self pushVc:vc];
    }
    
}
- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
    self.allMoney = 0.00;
}

/// <#Description#>
- (void)loadData {
//    @[@"全部",@"现付开票",@"现付开票",@"现付开票"]
//0-现付开票，1-到付开票，2-代付开票
    
//    if (self.indexPage.intValue == 0) {
//        self.strStatus = @"0";
//    }else if (self.indexPage.intValue == 1){
//        self.strStatus = @"1";
//    }else if (self.indexPage.intValue == 2){
//        self.strStatus = @"2";
//    }
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/invoice/translateInvoice",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"billingType":@"",
                            };

    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {

//        DLog(@"++++++++%@",responseObject);
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);

        if ([ws.refreshType isEqualToString:@"down"]) {
            [ws.modelArray removeAllObjects];
        }

        [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSError * error ;
            InvoiceClaimListModel *model = [[InvoiceClaimListModel alloc]initWithDictionary:obj error:&error];
            model.selected = NO;
            [ws.modelArray addObject:model];
        }];

        
        ws.allMoney = 10.00;
        [ws.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            InvoiceClaimListModel *model = (InvoiceClaimListModel *)obj;
            if (model.selected) {
                ws.allMoney = ws.allMoney + model.shipperDeliveryFee;
            }
        }];
        
        ws.labSumMoney.text = CJStringWithFormat(@"运费金额:%@元",@(ws.allMoney).cj_money_description);
        ws.labInvioceMoney.text = CJStringWithFormat(@"发票金额:%@元",@(ws.allMoney).cj_money_description);
        
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];

        if (ws.modelArray.count == total.intValue) {
            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [ws.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];

}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    self.allMoney = 0.00;
    self.labSumMoney.text = CJStringWithFormat(@"运费金额:%@元",@(self.allMoney).cj_money_description);
    self.labInvioceMoney.text = CJStringWithFormat(@"发票金额:%@元",@(self.allMoney).cj_money_description);
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"InvoiceClaimListCell";
    InvoiceClaimListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[InvoiceClaimListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    InvoiceClaimListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoModel:model];
    
    WS(ws);
    cell.blockSeclect = ^(id anyObject) {
        if (model.selected) {
            model.selected = NO;
        }else{
            model.selected = YES;
        }
        [ws.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
        ws.allMoney = 0.00;
        [ws.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            InvoiceClaimListModel *model = (InvoiceClaimListModel *)obj;
            if (model.selected) {
                ws.allMoney = ws.allMoney + model.shipperDeliveryFee;
            }
        }];
        
        ws.labSumMoney.text = CJStringWithFormat(@"运费金额:%@元",@(ws.allMoney).cj_money_description);
        ws.labInvioceMoney.text = CJStringWithFormat(@"发票金额:%@元",@(ws.allMoney).cj_money_description);
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    InvoiceClaimListModel *model = self.modelArray[indexPath.row];
//    if (model.selected) {
//        model.selected = NO;
//    }else{
//        model.selected = YES;
//    }
//    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
//    WS(ws);
//    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        InvoiceClaimListModel *model = (InvoiceClaimListModel *)obj;
//        if (model.selected) {
//            ws.allMoney = ws.allMoney + model.shipperDeliveryFee;
//        }
//    }];
//
//    self.labSumMoney.text = CJStringWithFormat(@"运费金额:%0.2f元",self.allMoney);
//    self.labInvioceMoney.text = CJStringWithFormat(@"发票金额:%0.2f元",self.allMoney);

    InvoiceClaimListModel *model = self.modelArray[indexPath.row];
    if (model.selected) {
        model.selected = NO;
    }else{
        model.selected = YES;
    }
    [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    self.allMoney = 0.00;
    WS(ws);
    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        InvoiceClaimListModel *model = (InvoiceClaimListModel *)obj;
        if (model.selected) {
            ws.allMoney = ws.allMoney + model.shipperDeliveryFee;
        }
    }];
    
    self.labSumMoney.text = CJStringWithFormat(@"运费金额:%@元",@(self.allMoney).cj_money_description);
    self.labInvioceMoney.text = CJStringWithFormat(@"发票金额:%@元",@(self.allMoney).cj_money_description);
}

- (void)pushVc:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (CJTableView *)tableView{
    
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight +header.bottom + LW(15), SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight - header.height - LW(15) - 44),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    
    return _tableView;
}

@end
